package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.entity.*;
import com.itheima.service.Service.CategoryService;
import com.itheima.service.Service.DishService;
import com.itheima.service.Service.SetmealDishService;
import com.itheima.service.Service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
//套餐表
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealDishService setmealDishService;
    @GetMapping("/page")
    public R page(HttpServletRequest request, int page, int pageSize, String name){
        Page<Setmeal> pageInfo=new Page<>(page,pageSize);
        Page<SetmealDto> pageDeto=new Page<>();
        LambdaQueryWrapper<Setmeal> lqw=new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name),Setmeal::getName,name).orderByAsc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo,lqw);
        List<Setmeal> setmealList=pageInfo.getRecords();
        List<SetmealDto> setmealDtoList=new ArrayList<>();
        for (Setmeal setmeal : setmealList) {
            SetmealDto setmealDto=new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);
            //查询category分类表获取分类名称并注入
            setmealDto.setCategoryName(categoryService.getById(setmeal.getCategoryId()).getName());
            LambdaQueryWrapper<SetmealDish> sdlqw=new LambdaQueryWrapper<>();
            log.info("setmealId:{}",setmealDto.getId());
            sdlqw.eq(SetmealDish::getSetmealId,setmealDto.getId()).orderByAsc(SetmealDish::getUpdateTime);
            List<SetmealDish> setmealDishList = setmealDishService.list(sdlqw);
            setmealDto.setSetmealDishes(setmealDishList);
            setmealDtoList.add(setmealDto);
        }
        pageDeto.setRecords(setmealDtoList);
        pageDeto.setTotal(pageInfo.getTotal());
        return R.success(pageDeto);
    }
    //根据id回显
    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable long id){
        SetmealDto setmealDto=new SetmealDto();
        Setmeal setmeal = setmealService.getById(id);
        BeanUtils.copyProperties(setmeal,setmealDto);
        String categoryName = categoryService.getById(setmeal.getCategoryId()).getName();
        setmealDto.setCategoryName(categoryName);
        LambdaQueryWrapper<SetmealDish> sdlqw=new LambdaQueryWrapper<>();
        sdlqw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishList = setmealDishService.list(sdlqw);
        setmealDto.setSetmealDishes(setmealDishList);
        return R.success(setmealDto);
    }
    //修改套餐
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDto,setmeal);
        setmealService.updateById(setmeal);
        LambdaQueryWrapper<SetmealDish> sdlqw = new LambdaQueryWrapper<>();
        sdlqw.eq(SetmealDish::getSetmealId,setmeal.getId());

        LambdaQueryWrapper<Dish> dslqw = new LambdaQueryWrapper<>();
        for (SetmealDish setmealDish : setmealDto.getSetmealDishes()) {
            setmealDish.setSetmealId(String.valueOf(setmeal.getId()));
            setmealDishService.save(setmealDish);
            Dish dish = dishService.getById(setmealDish.getDishId());
            dish.setStatus(1);
            dishService.updateById(dish);
        }
        return R.success("修改成功");
    }
    @CacheEvict(value = "setmealCache",allEntries = true)
    //添加
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
//        Setmeal setmeal = new Setmeal();
//        BeanUtils.copyProperties(setmealDto,setmeal);
//        setmealService.save(setmeal);
        setmealService.save(setmealDto);
        List<SetmealDish> setmealDishList = new ArrayList<>();
        for (SetmealDish setmealDish : setmealDto.getSetmealDishes()) {
                setmealDish.setSetmealId(String.valueOf(setmealDto.getId()));
                setmealDish.setDishId(String.valueOf(setmealDish.getDishId()));
                setmealDishList.add(setmealDish);
//            setmealDishService.save(setmealDish);
        }
        setmealDishService.saveBatch(setmealDishList);
        return R.success("添加成功");
    }
    //刪除
    @CacheEvict(value = "setmealCache",allEntries = true)
    @DeleteMapping()
    public R<String> remove(@RequestParam List<Long> ids){
        log.info("ids:{}",ids);
        setmealService.removeByIds(ids);
        LambdaQueryWrapper<SetmealDish> sdlqw = new LambdaQueryWrapper<>();
        sdlqw.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(sdlqw);
        return R.success("刪除成功");
    }
    // 启售/停售
    @PostMapping("/status/{status}")
    public R<String> updateSttatus(@PathVariable Integer status,@RequestParam List<Long> ids){
        for (Long id : ids) {
            Setmeal setmeal = setmealService.getById(id);
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }
        return R.success("修改状态成功");
    }
    //根据条件查询套餐数据
    @Cacheable(value = "setmealCache",key = "'setmeal'+#setmeal.categoryId+'_'+#setmeal.status")
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }
}
